package com.zx.wangyixinwen.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.zx.wangyixinwen.R;
import com.zx.wangyixinwen.activity.FoundActivity;
import com.zx.wangyixinwen.adapter.FoundAdapter;
import com.zx.wangyixinwen.bean.Found;
import com.zx.wangyixinwen.refresh.SwipeRefreshLoadingLayout;

import java.util.ArrayList;
import java.util.List;



/**
 * Created by dllo on 16/1/4.
 */
public class FoundFragment extends Fragment implements AdapterView.OnItemClickListener,SwipeRefreshLoadingLayout.OnRefreshListener,SwipeRefreshLoadingLayout.OnLoadListener {
    private SwipeRefreshLoadingLayout swipeRefreshLoadingLayout;
    private List<Found.DataEntity.ChannelGroupsEntity> datas;
    private GridView gridView;
    private RequestQueue queue;
    private FoundAdapter adapter;
    int i = 0;
    private String url = "http://api.liwushuo.com/v2/items?limit=20&offset=0&gender=1&generation=1";

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_see, null);
        gridView = (GridView) view.findViewById(R.id.gridView);
        adapter = new FoundAdapter(getActivity());
        gridView.setAdapter(adapter);
        gridView.setOnItemClickListener(this);
        queue = Volley.newRequestQueue(getActivity());
        datas = new ArrayList<Found.DataEntity.ChannelGroupsEntity>();
        swipeRefreshLoadingLayout = (SwipeRefreshLoadingLayout) view.findViewById(R.id.swipeRefreshLoadingLayout);
        swipeRefreshLoadingLayout.setOnRefreshListener(this);
        swipeRefreshLoadingLayout.setOnLoadListener(this);
        StringRequest request = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                if (response != null) {
                    Gson gson = new Gson();
                    Found result = gson.fromJson(response, Found.class);
                    if (result != null && result.getData() != null) {
                        datas = result.getData().getItems();
                        adapter.initData(datas);


                    }
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {

            }
        });
        queue.add(request);

        return view;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent intent = new Intent(getActivity(), FoundActivity.class);
        String url = datas.get(position).getData().getUrl();
        String name = datas.get(position).getData().getName();
        intent.putExtra("url", url);
        intent.putExtra("name", name);
        startActivity(intent);

    }

    @Override
    public void onRefresh() {

        StringRequest request = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                if (response != null) {

                    Gson gson = new Gson();
                    Found result = gson.fromJson(response, Found.class);
                    if (result != null && result.getData() != null) {
                        datas = result.getData().getItems();
                        adapter.initData(datas);


                    }
                }
            }

        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {

            }
        });
        swipeRefreshLoadingLayout.setRefreshing(false);
        queue.add(request);

    }

    @Override
    public void onLoad() {
        i += 20;
        String urll = "http://api.liwushuo.com/v2/items?limit=20&offset=" + i + "&gender=1&generation=1";
        StringRequest request = new StringRequest(urll, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                if (response != null) {
                    Gson gson = new Gson();
                    Found result = gson.fromJson(response, Found.class);
                    if (result != null && result.getData() != null) {
                        datas = result.getData().getItems();
                        adapter.initData(datas);


                    }
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {

            }
        });
        swipeRefreshLoadingLayout.setLoading(false);
        queue.add(request);

    }
}
